package com.voicechat.firstmet.view;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.voicechat.firstmet.R;


public class SimpleViewPagerIndicatorLook extends LinearLayout {

	private static final int COLOR_TEXT_NORMAL = 0xFF666666;
	private static final int COLOR_INDICATOR_COLOR = 0xFF333333;


	private String[] mTitles;
	private int mTabCount = 1;
	private int mIndicatorColor = 0xFFFFB1BF;
	private float mTranslationX;
	private Paint mPaint = new Paint();
	private int mTabWidth;
	private LazyViewPager viewPager;
	private int oldPosition = 0;
	private boolean isFirst=true;
	private String TAG = "SimpleViewPagerIndicatorLook";
	public SimpleViewPagerIndicatorLook(Context context) {
		this(context, null);
	}

	public SimpleViewPagerIndicatorLook(Context context, AttributeSet attrs) {
		super(context, attrs);
		mPaint.setColor(mIndicatorColor);
		mPaint.setStrokeWidth(getResources().getDimension(R.dimen.x10));
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		if(isFirst){
			mTabWidth =w / mTabCount-getResources().getDimensionPixelSize(R.dimen.x90);
			isFirst=false;
			//Log.i(TAG, "onSizeChanged: --------"+mTabWidth);
		}
		
	}

	public void setTitles(String[] titles) {
		mTitles = titles;
		mTabCount = titles.length;
		generateTitleView();

	}

	public void setIndicatorColor(int indicatorColor) {
		this.mIndicatorColor = indicatorColor;
	}

	@SuppressWarnings("deprecation")
	public void setViewPager(LazyViewPager viewPager){
		this.viewPager = viewPager;
		this.viewPager.setOnPageChangeListener(new LazyViewPager.OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int arg0) {
				((TextView) getChildAt(oldPosition)).setTextColor(COLOR_TEXT_NORMAL);
				((TextView) getChildAt(arg0)).setTextColor(COLOR_INDICATOR_COLOR);
				oldPosition = arg0;
				if (onPageChangeListener != null)  
                {  
                    onPageChangeListener.onPageSelected(arg0);  
                }  
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				scroll(arg0, arg1);
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				
			}
		});
	}
	@Override
	protected void dispatchDraw(Canvas canvas) {
		super.dispatchDraw(canvas);
		canvas.save();
		canvas.translate(mTranslationX, getHeight() - 2);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
		canvas.drawLine(getResources().getDimensionPixelSize(R.dimen.x90),
				-getResources().getDimensionPixelSize(R.dimen.x20), mTabWidth,
				-getResources().getDimensionPixelSize(R.dimen.x20), mPaint);
		canvas.restore();
	}

	public void scroll(int position, float offset) {
		/**
		 * <pre>
		 *  0-1:position=0 ;1-0:postion=0;
		 * </pre>
		 */
		mTranslationX = getWidth()/mTabCount * (position + offset);
		invalidate();
		
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		return super.dispatchTouchEvent(ev);
	}

	private void generateTitleView() {
		if (getChildCount() > 0)
			this.removeAllViews();
		int count = mTitles.length;

		setWeightSum(count);
		for (int i = 0; i < count; i++) {
			TextView tv = new TextView(getContext());
			LayoutParams lp = new LayoutParams(0, LayoutParams.MATCH_PARENT);
			lp.weight = 1;
			tv.setTag(i);
			tv.setGravity(Gravity.CENTER);
			//字体加粗
			TextPaint tp = tv.getPaint();
			tp.setFakeBoldText(true);
			if (i == 0) {
				tv.setTextColor(COLOR_INDICATOR_COLOR);
			}else {
				tv.setTextColor(COLOR_TEXT_NORMAL);
			}
			//tv.setBackgroundColor(R.color.asset_holding_gray);
			tv.setText(mTitles[i]);
			tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
			tv.setLayoutParams(lp);

			tv.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					int position = (Integer) arg0.getTag();
                    if (viewPager != null&&viewPager.getVisibility()!= View.GONE)
                    {  
                        viewPager.setCurrentItem(position);  
                        
                    }else{
                    	
                    }  
				}
			});
			addView(tv);
		}
		//setIndicator(0);
	}
	
	 private OnPageChangeListener onPageChangeListener;  
	  
	    public void setOnPageChangeListener(OnPageChangeListener onPageChangeListener)  
	    {  
	        this.onPageChangeListener = onPageChangeListener;  
	    }  
	  
	    public interface OnPageChangeListener  
	    {  
	        public void onPageSelected(int position);  
	    }  

	
	
	 public void setCurrentItem(int position)  
	    {  
	        oldPosition = position;  
	        if (viewPager != null)  
	        {  
	            viewPager.setCurrentItem(position);  
	        }  
	    }  
}
